﻿Imports System.Data.SqlClient

Public Class FrmDosCampos

    Private _ObjMethod As ClaseBaseMethod
    Private _Obj As ClaseDosCamposObj

    Private _permisos As PermisoObj
    Public Property Permisos() As PermisoObj
        Get
            Return _permisos
        End Get
        Set(ByVal Value As PermisoObj)
            _permisos = Value
        End Set
    End Property

    Private _tabla As ClaseEnumObj.Roles
    Public Property Tabla() As ClaseEnumObj.Roles
        Get
            Return _tabla
        End Get
        Set(ByVal Value As ClaseEnumObj.Roles)
            _tabla = Value
        End Set
    End Property

    Public Sub New(ByVal Rol As ClaseEnumObj.Roles)
        InitializeComponent()

        Try
            Tabla = Rol
            Select Case Tabla
                Case ClaseEnumObj.Roles.Formularios
                    Text = "Formularios"
                    StartPosition = FormStartPosition.CenterScreen
                    Permisos = PermisoMethod.RecuperarPermisos(ClaseEnumObj.Roles.Formularios, UsuarioActual)
                    Icon = CType(My.Resources.Formularios, System.Drawing.Icon)

                    _ObjMethod = New RolMethod
                    _Obj = New RolObj

                    If Not Permisos.Eliminar Then
                        BtnEliminar.Enabled = False
                    End If

                    If (Not Permisos.Incluir) And (Not Permisos.Editar) Then
                        BtnGrabar.Enabled = False
                    End If
            End Select

        Catch ex As SqlException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarPermisos, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dispose()
        Catch ex As ReglaNegociosMetodosException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlRecuperarPermisos, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dispose()
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Dispose()
        End Try
    End Sub

    Private Sub BtnGrabar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnGrabar.Click
        Try
            If TxtCodigo.EditValue = 0 Then

                If Not Permisos.Incluir Then
                    DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.PermisoDenegado, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Exit Sub
                End If

                If DatosCorrectos() Then
                    _Obj.Descripcion = TxtDescripcion.Text.Trim
                    _Obj = _ObjMethod.Insertar(_Obj)
                    TxtCodigo.EditValue = _Obj.Id
                End If
                
            Else

                If Not Permisos.Editar Then
                    DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.PermisoDenegado, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand)
                    Exit Sub
                End If

                If DatosCorrectos() Then
                    _Obj.Descripcion = TxtDescripcion.Text.Trim
                    _Obj.Id = CLng(TxtCodigo.EditValue)
                    _ObjMethod.Actualizar(_Obj)
                End If
                
            End If

            DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.OperacionRealizada, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch ex As SqlException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlInsertarEditarEliminar, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As ReglaNegociosMetodosException
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlInsertarEditarEliminar, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub BtnEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEliminar.Click
        If txtcodigo.editvalue<>0 Then
            
            Try

                If DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.ConfirmacionDeEliminar, My.Application.Info.ProductName, MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If

                If TxtCodigo.EditValue <> 0 Then
                    _Obj.Id = CLng(TxtCodigo.EditValue)
                    _ObjMethod.Eliminar(_Obj)
                End If

                DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.OperacionRealizada, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information)

                Me.BtnNuevo.BtnNuevo_Click(Nothing, Nothing)
            Catch ex As SqlException
                DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlInsertarEditarEliminar, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ex As ReglaNegociosMetodosException
                DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorAlInsertarEditarEliminar, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show(String.Format(My.Resources.ResMensajes.ErrorDesconocido, ex.Message), My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

    Private Function DatosCorrectos() As Boolean

        If String.IsNullOrEmpty(TxtDescripcion.Text) Then
            DevExpress.XtraEditors.XtraMessageBox.Show(My.Resources.ResMensajes.DescripcionNula, My.Application.Info.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Question)
            TxtDescripcion.Focus()
            Return False
        End If

        Return True
    End Function

    Private Sub BtnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBuscar1.Click
        Try
            Dim FormBuscar As New FrmBuscar()

            FrmBuscar.ShowDialog(_ObjMethod.ConfigurarBuscar)

            If BuscarObj.ElCodigoBuscado(0) = 1 Then
                TxtCodigo.Text = BuscarObj.ElCodigoBuscado(1)
                BuscarObj.ElCodigoBuscado(0) = 0
            End If

            'BtnActualizar_LinkClicked(Nothing, Nothing)

            TxtCodigo.Focus()
        Catch ex As Exception
            DevExpress.XtraEditors.XtraMessageBox.Show("Ocurrio Un Error: " & ex.Message)
        End Try
    End Sub
End Class